package com.newcoder.community.controller;


import com.newcoder.community.service.AlphaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@Controller
@RequestMapping("/alpha")
public class AlphaController {

    @Autowired
    private AlphaService alphaService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }

    @RequestMapping("/data")
    @ResponseBody
    public String getData(){
        return alphaService.find();
    }

    //在spring MVC怎么获得响应对象,请求对象
    @RequestMapping("http")
    public void http(HttpServletRequest request, HttpServletResponse response){
        //获取请求数据
        System.out.println(request.getMethod());//请求方法
        System.out.println(request.getServletPath());//请求路径
        Enumeration<String> enumeration = request.getHeaderNames();//年代比较久远的迭代器
        while(enumeration.hasMoreElements()){
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            System.out.println(name +" : "+ value);
        }
        System.out.println(request.getParameter("code"));

        //返回响应数据
        response.setContentType("text/html;charset=utf-8");
        try ( PrintWriter writer = response.getWriter();)//java7新增写法
        {
            writer.write("<h1>牛客网</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //GET请求，一般向服务器获取数据时使用
    //  /student?current=1&limit=20
    @RequestMapping(path = "/students" , method = RequestMethod.GET)
    @ResponseBody
    public String getStudents(
            @RequestParam(name = "current",required = false , defaultValue = "1") int current,
            @RequestParam(name = "limit",required = false , defaultValue = "20") int limit){
        System.out.println(current);
        System.out.println(limit);

        return "some students";
    }

    //  /student/123
    @RequestMapping(path = "/student/{id}",method= RequestMethod.GET)
    @ResponseBody
    public String getStudent(@PathVariable("id") int id){
        System.out.println(id);
        return "a student";
    }


    //post请求
    @RequestMapping(path = "/student",method = RequestMethod.POST)
    @ResponseBody
    public String saveStudent(String name,int age){
        System.out.println(name);
        System.out.println(age);
        return "success";
    }

    //向浏览器响应动态的html
    @RequestMapping(path = "/teacher",method = RequestMethod.GET)
    public ModelAndView getTeacher(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("name","彭凡");
        mav.addObject("age","25");
        mav.setViewName("/demo/view");//设置模板路径及名称

        return mav;
    }

    @RequestMapping(path = "/school",method = RequestMethod.GET)
    public String getSchool(Model model){
        model.addAttribute("name","hujin");
        model.addAttribute("age",100);
        return "/demo/view";
    }

    //响应JSON数据（异步请求）
    //Java对象 -转化成，并传输给浏览器-> JSON字符串 -接收解析转化-> JS对象
    @RequestMapping(path = "/emp" ,method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> emp(){
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",25);
        return map;
    }

    @RequestMapping(path = "/emps" ,method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> emps(){
        List<Map<String,Object>> emps = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",25);
        emps.add(map);

        map = new HashMap<>();
        map.put("name","张");
        map.put("age",222);
        emps.add(map);

        map = new HashMap<>();
        map.put("name","三");
        map.put("age",122);
        emps.add(map);
        return emps;
    }


}
